import os
import getopt, sys
import getpass

def encode():
	psswrd1 = getpass.getpass()
	print "Confirm ",
	psswrd2 = getpass.getpass()
	if psswrd1 != psswrd2:
		print 'The password is not at the same.'
		sys.exit(2)

	print 'start encode...'
	dr = os.walk('.')
	for root, dirs, files in dr:
		for fl in files:
			if fl.endswith('.src'):
				cmd = 'openssl enc -aes-256-cbc -salt -in ' + fl + ' -out ' + fl.replace('.src', '.enc') + ' -pass pass:' + psswrd1
				print cmd
				os.system(cmd)
	os.system('rm -f *.src')

def decode():
	psswrd = getpass.getpass()
	print 'start decode...'
	dr = os.walk('.')
	for root, dirs, files in dr:
		for fl in files:
			if fl.endswith('.enc'):
				cmd = 'openssl enc -d -aes-256-cbc -in ' + fl + ' > ' + fl.replace('.enc', '.src') + ' -pass pass:' + psswrd
				print cmd
				os.system(cmd)

if __name__ == "__main__":
	try:
		opts, args = getopt.getopt(sys.argv[1:], "de")
	except getopt.GetoptError, err:
		sys.exit(2)
	for opt, arg in opts:
		if opt == '-d':
			decode()
		elif opt == '-e':
			encode()

